package com.glorysoft.xztpda.ui.changeshift

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.glorysoft.xztpda.entity.ui.ToastMessageBean
import com.glorysoft.xztpda.BR
import com.glorysoft.xztpda.R
import com.glorysoft.xztpda.base.BaseActivity
import com.glorysoft.xztpda.databinding.ActivityChangeShiftBinding
import com.glorysoft.xztpda.entity.ChangeShiftEntity
import com.huawei.hms.hmsscankit.ScanUtil
import com.huawei.hms.ml.scan.HmsScan
import kotlinx.android.synthetic.main.activity_change_shift.*
import kotlinx.android.synthetic.main.activity_change_shift.etLotID
import kotlinx.android.synthetic.main.activity_change_shift.ivLotScan
import kotlinx.android.synthetic.main.activity_track_in_guide.*

/**
 * FileName: ChangeShift2Activity
 * Author: XJH
 * Date: 2022/5/26 17:21
 * Description:
 * History:
 */
class ChangeShift2Activity : BaseActivity<ChangeShiftViewModel, ActivityChangeShiftBinding>() {
    override fun layoutId(): Int {
        return R.layout.activity_change_shift
    }

    override fun initVariableId(): Int {
        return BR.changeShiftViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle("接班")
        llCommentLayout.visibility = View.GONE
        llSubQty.visibility = View.VISIBLE
        setRightListener("提交") {
            if (viewModel.shift2Items.size <= 0) {
                viewModel.defUI.toastEvent.value = ToastMessageBean("请添加列表信息后操作", false)
                return@setRightListener
            }

            viewModel.shift2()
        }

        ivLotScan.setOnClickListener{
            startScanForCode(this,10001)
        }
    }

    override fun initData() {
//        viewModel.initShift()

        initEdit(etLotID,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.initShift(s)
                etLotID.setText("")
            }

        })

        viewModel.defUI.callObserve.observe(this){
            if(it.type == 1){
                val data = it.item as ChangeShiftEntity
                tvSubmitQty.text = data.sumstepqty.toString()
            }else{
                finish()
            }

        }

        viewModel.defUI.startActivity.observe(this){
            val currentFocus = currentFocus
            currentFocus?.clearFocus()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        // 当扫码页面结束后，处理扫码结果。
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != RESULT_OK || data == null) {
            return
        }
        if (resultCode == RESULT_OK){
            // 从onActivityResult返回data中，用ScanUtil.RESULT作为key值取到HmsScan返回值。
            when (val obj: Any = data.getParcelableExtra(ScanUtil.RESULT)) {
                is HmsScan -> {
                    val barCode = obj.getOriginalValue()
                    when(requestCode){
                        10001 -> {
                            viewModel.initShift(barCode)
                        }
                    }
                }
            }


        }
    }
}